package com.sumer.brain.common.model;

import com.sumer.brain.common.enums.HttpStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(name = "R", description = "统一返回实体")
public class R<T> {

    @Schema(title = "状态码", description = "响应的HTTP状态码", type = "integer")
    private int code;

    @Schema(title = "描述", description = "响应信息的描述", type = "string")
    private String msg;

    @Schema(title = "内容", description = "响应的内容")
    private T data;

    public static <T> R<T> ok() {
        return build(HttpStatus.OK.getCode(), HttpStatus.OK.getMsg(), null);
    }

    public static <T> R<T> ok(T data) {
        return build(HttpStatus.OK.getCode(), HttpStatus.OK.getMsg(), data);
    }

    public static <T> R<T> ok(String msg, T data) {
        return build(HttpStatus.OK.getCode(), msg, data);
    }

    public static <T> R<T> fail() {
        return build(HttpStatus.FAIL.getCode(), HttpStatus.FAIL.getMsg(), null);
    }

    public static <T> R<T> fail(String msg) {
        return build(HttpStatus.FAIL.getCode(), msg, null);
    }

    public static <T> R<T> fail(T data) {
        return build(HttpStatus.FAIL.getCode(), HttpStatus.FAIL.getMsg(), data);
    }

    public static <T> R<T> fail(String msg, T data) {
        return build(HttpStatus.FAIL.getCode(), msg, data);
    }

    private R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> build(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }
}
